package cn.maihe.elg.operation.schedule;

import cn.maihe.elg.operation.common.Constants;
import cn.maihe.elg.operation.model.vo.system.CaptchaVO;
import cn.maihe.elg.operation.utils.SystemClock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

/**
 * @Description
 * @Author caixh
 * @Date 2021/6/8.
 */
@Slf4j
@Component
public class CaptchaCacheClearSchedule {

    @Scheduled(fixedDelay = 60000)
    public void clearCache(){
        log.debug("begin clear,captcha size:{}", Constants.CAPTCHA_HASHMAP.size());

        long time = SystemClock.now();
        Set<Map.Entry<String, CaptchaVO>> entrySet = Constants.CAPTCHA_HASHMAP.entrySet();
        entrySet.removeIf(entry -> entry.getValue().getTtlTime()<time);
        log.debug("end clear,captcha size:{}", Constants.CAPTCHA_HASHMAP.size());
    }

}
